/**
 * store.js
 * @authors 杨兴洲（of2502）
 * @date    2017/5/26 0026 10:16
 * @version 1.0
 * @last-modify  杨兴洲（of2502）
 */


import {Store} from 'iflux2';
import {fromJS} from 'immutable';
import ReplyActor from './actor/reply-actor.js';
import * as types from './constants/actionTypes.js'
import WebApi from './webapi.js';
import {message} from 'antd';

export default class ReplyStore extends Store {
    bindActor() {
        return [
            new ReplyActor,
        ]
    }

    fetchFollowReply = async () => {
        try {
            const data = await WebApi.fetchFollowReply();
            // console.log("关注时回复", data);
            this.dispatch(types.FETCH_FOLLOW_REPLY, data);
            return data;
        } catch (error) {
            console.error(error)
        }

    };

    updateFollowReply = async (params) => {
        try {
            const data = await WebApi.updateFollowReply(params);
            // console.log("关注时回复更新规则", data);
            message.success(`更新关注时回复内容成功`);
            // this.dispatch(types.UPDATE_FOLLOW_REPLY, data);
            return data;
        } catch (error) {
            console.error(error)
        }
    };

    deleteFollowReply = async () => {
        try {
            const data = await WebApi.deleteFollowReply();
            // console.log("关注时回复更新规则", data);
            message.success(`更新关注时回复内容成功`);
            this.dispatch(types.DELETE_FOLLOW_REPLY);
            return data;
        } catch (error) {
            console.error(error)
        }
    };

    fetchKeywordReplyList = async ({page, pageSize}) => {
        try {
            const {total, list} = await WebApi.fetchKeywordReplyList({page, pageSize});
            // console.log("关键词列表", total, list);
            this.dispatch(types.FETCH_KEYWORD_REPLY_LIST, {
                list: fromJS(list),
                total,
                pageNo: page,
                pageSize
            });
            return {total, list};
        } catch (error) {
            console.error(error)
        }

    };

    createKeywordReplyRule = async (rule) => {
        try {
            const result = await WebApi.createKeywordReply(rule);
            // console.log("createKeywordReplyRule", result);
            this.dispatch(types.INIT_PAGINATION);
            return result;
        } catch (error) {
            console.error(error);
        }
    };

    updateKeywordReplyRule = async ({ruleID, rule}) => {
        try {
            const result = await WebApi.updateKeywordReply({ruleID, rule});
            // console.log("updateKeywordReplyRule", result);
            message.success(`更新规则${rule.rule_name}成功`);
            return result;
        } catch (error) {
            console.error(error);
        }
    };

    deleteKeywordReplyRule = async (ruleID) => {
        try {
            const result = await WebApi.deleteKeywordReply(ruleID);
            // console.log("deleteKeywordReplyRule", result);
            return result;
        } catch (error) {
            console.error(error);
        }
    };

    getMPNewsDetail = async (media_id) => {
        try {
            const result = await WebApi.getMPNewsDetail(media_id);
            // console.log("getMPNewsDetail", result);
            return result;
        } catch (error) {
            console.error(error);
        }
    };

}